<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2-axios基本使用</title>
    <link crossorigin="anonymous" href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
</head>
<body>
    <div class="container">
        <h2 class="page-header">基本使用</h2>
        <button class="btn btn-primary"> 发送GET请求 </button>
        <button class="btn btn-warning" > 发送POST请求 </button>
        <button class="btn btn-success"> 发送 PUT 请求 </button>
        <button class="btn btn-danger"> 发送 DELETE 请求 </button>
    </div>

    <script>
        //获取按钮
        const btns=document.querySelectorAll('.btn');

        // //第一个
        // btns[0].onclick=function(){
        //     //发送 AJAX 请求
        //     let aa=axios({
        //         //请求类型
        //         method:'get',
        //         //URL
        //         url:'http://localhost:3000/posts/1',
        //     });

        //     console.log(aa); //返回的是Promise对象

        //     //发送 AJAX 请求
        //     axios({
        //         //请求类型
        //         method:'get',
        //         //URL
        //         url:'http://localhost:3000/posts/1',
        //     }).then(response=>{
        //         console.log(response);
        //         console.log(response.data);
        //     });
        // };


        btns[0].onclick = async function(){

            //发两次请求
            let res1=await axios({
                method:'get',
                url:'http://localhost:3000/posts/1'
            });

            let res2=await axios({
                method:'get',
                url:'http://localhost:3000/posts/2'
            });

            console.log(res1);
            console.log(res2);
            
        };

        //添加一篇新文章
        btns[1].onclick=function(){
            
            axios({
                method:'post',
                url:'http://localhost:3000/posts',
                data:{
                    "title":"标题一下",
                    "author":"作者一下"
                }
            }).then(response=>{
                console.log(response);
            });

            axios({
                method:'post',
                url:'http://localhost:3000/posts',
                data:"title=哈哈&author=中华人民共和国",
            }).then(response=>{
                console.log(response);
            });
        };

          //修改一篇新文章
          btns[2].onclick=function(){
            
            axios({
                method:'put',
                url:'http://localhost:3000/posts/3',
                data:{
                    "title":"标题一下111",
                    "author":"作者一下222"
                }
            }).then(response=>{
                console.log(response);
            });

        };

        //删除
        btns[3].onclick=function(){

            axios({
                method:'delete',
                url:'http://localhost:3000/posts/3',
            }).then(response=>{
                console.log(response);
            });
            
        };

    </script>
</body>
</html>